Extend a Command Factory

As the Command Factory classes are generated as partial classes, it is easy to extend them with some new queries.

Please follow these steps to create a new command that will find all Employee objects with a certain last name and a certain first name:



using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;


namespace MyProject.BusinessLogic.BusinessObjects.Commands

{

    internal static partial class EmployeeCommands

    {

    }

}


Notice that the namespace is the same as in the generated class EmployeeCommands (in the Generated folder). So now the EmployeeCommands class is devided into two parts, the generated part and the part for individual code that you have created.


After that, your current solution structure should look like this:




As next, include the following method into the EmployeeCommands class:


SQL Server:


/// <summary>

/// Finds all Employee objects with a certain last name and first name.

/// </summary>

/// <param name="lastname">The last name ('*' can be used as a wildcard).</param>

/// <param name="firstname">The first name ('*' can be used as a wildcard).</param>

/// <returns>An IDbCommand that finds all Employee objects with a certain last name and first name.</returns>

public static IDbCommand FindByFullName(string lastname, string firstname)

{

    SqlCommand cmd = new SqlCommand(

        String.Format(

        "select * from [{0}].[{1}] where ([{2}] like @lastname) and ([{3}] like @firstname)",

        Employee.SchemaName,

        Employee.TableName,

        Employee.ColumnNames.Lastname,

        Employee.ColumnNames.Firstname));


    cmd.Parameters.AddWithValue("@lastname", lastname.Replace("*", "%"));

    cmd.Parameters.AddWithValue("@firstname", firstname.Replace("*", "%"));


    return cmd;

}



Access:


/// <summary>

/// Finds all Employee objects with a certain last name and first name.

/// </summary>

/// <param name="lastname">The last name ('*' can be used as a wildcard).</param>

/// <param name="firstname">The first name ('*' can be used as a wildcard).</param>

/// <returns>An IDbCommand that finds all Employee objects with a certain last name and first name.</returns>

public static IDbCommand FindByFullName(string lastname, string firstname)

{

    OleDbCommand cmd = new OleDbCommand(

        String.Format(

        "select * from [{0}].[{1}] where ([{2}] like @lastname) and ([{3}] like @firstname)",

        Employee.SchemaName,

        Employee.TableName,

        Employee.ColumnNames.Lastname,

        Employee.ColumnNames.Firstname));


    cmd.Parameters.AddWithValue("@lastname", lastname.Replace("*", "%"));

    cmd.Parameters.AddWithValue("@firstname", firstname.Replace("*", "%"));


    return cmd;

}



Now, you have extended the Employee Command Factory with a new command. This command can now be used in the next section Extend a Data Object.